package com.wxg.address.view;

import com.wxg.address.model.Person;
import com.wxg.address.util.DateUtil;

import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

/**
 * Dialog to edit details of a person.
 *
 * @author Wxg
 *
 */
public class PersonEditDialogController {
	@FXML
	private TextField firstNameField;
	@FXML
	private TextField lastNameField;
	@FXML
	private TextField streetField;
	@FXML
	private TextField postalCodeField;
	@FXML
	private TextField cityField;
	@FXML
	private TextField birthdayField;

	private Stage dialogStage;
	private Person person;
	private boolean okClicked = false;

	/**
	 * Initializes the controller class. This method is automatically called
	 * after the fxml file has been loaded.
	 */
	@FXML
	private void initialize(){}

	/**
	 * Sets the stage of this dialog.
	 *
	 * @param dialogStage
	 */
	public void setDialogStage(Stage dialogStage) {
		this.dialogStage = dialogStage;
	}

	/**
	 * Sets the person to be edited in the dialog.
	 *
	 * @param person
	 */
	public void setPerson(Person person) {
		this.person = person;

		firstNameField.setText(person.getFirstName().getValue());
		lastNameField.setText(person.getLastName().getValue());
		streetField.setText(person.getStreet().getValue());
		postalCodeField.setText(person.getPostalCode().getValue().toString());
		cityField.setText(person.getCity().getValue());
		birthdayField.setText(DateUtil.format(person.getBirthday().getValue()));
		birthdayField.setPromptText("dd.mm.yyyy");
	}

	public boolean isOkClicked() {
		return okClicked;
	}

	/**
	 * Called when the user clicks ok.
	 */
	@FXML
	private void handleOk(){
		if(isInputValid()){
			person.setFirstName(firstNameField.getText());
			person.setLastName(lastNameField.getText());
			person.setStreet(streetField.getText());
			person.setPostalCode(postalCodeField.getText());
			person.setCity(cityField.getText());
			person.setBirthday(birthdayField.getText());

			okClicked = true;
			dialogStage.close();
		}
	}

	/**
	 * Called when the user clicks cancel.
	 */
	@FXML
	private void handleCancel(){
		dialogStage.close();
	}

	private boolean isInputValid(){
		String errorMessage = "";

		if(firstNameField.getText() == null || firstNameField.getText().length() == 0){
			errorMessage += "No valid first name!\n";
		}
		if(lastNameField.getText() == null || lastNameField.getText().length() == 0){
			errorMessage += "No valid last name!\n";
		}
		if(streetField.getText() == null || streetField.getText().length() == 0){
			errorMessage += "No valid street!\n";
		}
		if(postalCodeField.getText() == null || postalCodeField.getText().length() == 0){
			errorMessage += "No valid postal code!\n";
		}else{
			try {
				Integer.parseInt(postalCodeField.getText());
			} catch (NumberFormatException e) {
				errorMessage += "No valid postal code (must be integer)!\n";
			}
		}
		if(cityField.getText() == null || cityField.getText().length() == 0){
			errorMessage += "No valid city!\n";
		}
		if(birthdayField.getText() == null || birthdayField.getText().length() == 0){
			errorMessage += "No valid birthday!\n";
		}else{
			if(!DateUtil.validDate(birthdayField.getText())){
				errorMessage += "No valid birthday. Use the format dd.mm.yyyy!\n";
			}
		}

		if(errorMessage.length() == 0){
			return true;
		}else{
			// Show the error message.
			Alert alert = new Alert(AlertType.INFORMATION);
			alert.setTitle("Invalid Fields");
			alert.setHeaderText("Please correct invalid fields");
			alert.setContentText(errorMessage);

			alert.showAndWait();
			return false;
		}
	}

}
